package com.zhkj.txg.utils

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView

/**
 * 设置GridlayoutManager的左右间距
 */
class GridItemDecoration(
    private val outDecorationPadding: Int,
    private val inDecorationPadding: Int,
    private val padding: Boolean = false
) : RecyclerView.ItemDecoration() {

    private var realItemWidth: Int = 0

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        val lm = parent.layoutManager as? GridLayoutManager
        if (lm != null) {
            val sizeLookup = lm.spanSizeLookup
            val position = lm.getPosition(view)
            val spanSize = sizeLookup.getSpanSize(position)
            // 多个item一行，可以设置间距
            if (spanSize == 1) {
                val spanCount = lm.spanCount
                realItemWidth =
                    (parent.measuredWidth - outDecorationPadding * 2 - (spanCount - 1) * inDecorationPadding) / spanCount
                when (sizeLookup.getSpanIndex(position, spanCount)) {
                    // 在一行的第一列,只绘制左边的间距
                    0 -> if (padding) {
                        view.setPadding(
                            outDecorationPadding,
                            0,
                            parent.measuredWidth / spanCount - realItemWidth - outDecorationPadding,
                            0
                        )
                    } else outRect.set(
                        outDecorationPadding,
                        0,
                        parent.measuredWidth / spanCount - realItemWidth - outDecorationPadding,
                        0
                    )
                    // 在一行的最后一列,只绘制右边的间距
                    spanCount - 1 -> if (padding) {
                        view.setPadding(
                            parent.measuredWidth / spanCount - realItemWidth - outDecorationPadding,
                            0,
                            outDecorationPadding,
                            0
                        )
                    } else outRect.set(
                        parent.measuredWidth / spanCount - realItemWidth - outDecorationPadding,
                        0,
                        outDecorationPadding,
                        0
                    )
                    // 其他位置，两边都设置间距
                    else -> if (padding) {
                        view.setPadding(
                            (parent.measuredWidth / spanCount - realItemWidth) / 2,
                            0,
                            (parent.measuredWidth / spanCount - realItemWidth) / 2,
                            0
                        )
                    } else outRect.set(
                        (parent.measuredWidth / spanCount - realItemWidth) / 2,
                        0,
                        (parent.measuredWidth / spanCount - realItemWidth) / 2,
                        0
                    )
                }
            }
        }
    }
}